/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pb.diplom.first.dao;

import com.pb.diplom.first.tables.Producer;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Slava
 */
public class ProducerDao {

    private final String login;
    private final String password;
    private final String url;

    public ProducerDao(String login, String password, String url) {
        this.login = login;
        this.password = password;
        this.url = url;
    }

    public void save(Producer producer) throws SQLException {
        Connection con = null;
        Statement stmt = null;
        try {
            con = DriverManager.getConnection(url, login, password);
            stmt = con.createStatement();
            stmt.executeUpdate("INSERT INTO Manufacturer values(" + producer.getId() + ", '" + producer.getName() + "' )");
        } finally {
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
    }

    public void save1(Producer producer) throws SQLException {
        Connection con = null;
        Statement stmt = null;
        CallableStatement proc=null;
        try {
            con = DriverManager.getConnection(url, login, password);
            proc = con.prepareCall(" { call insertManufacturer(?) }");
            proc.setString(1, producer.getName());
            proc.execute();

        } finally {
            if (stmt != null) {
                stmt.close();
            }
            if(proc != null){
                proc.close();
            }
            if (con != null) {
                con.close();
            }
        }
    }

    public List<Producer> getAll() throws SQLException {
        List<Producer> manufacturers = new ArrayList<Producer>();
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, login, password);
            stmt = con.prepareStatement("SELECT * FROM Manufacturer ORDER BY ID_manufacturer");
            rs = stmt.executeQuery();
            while (rs.next()) {
                Producer producer = new Producer();
                producer.setId(rs.getLong(1));
                producer.setName(rs.getString(2));
                manufacturers.add(producer);
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return manufacturers;
    }
    
    public Long getId(Producer proc) throws SQLException {
        Long l = null;
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            //Long l;
            con = DriverManager.getConnection(url, login, password);
            stmt = con.prepareStatement("SELECT ID_manufacturer FROM Manufacturer WHERE name_manufacturer= '"+proc.getName()+"'");
            rs = stmt.executeQuery();
            while (rs.next()) {
                Producer producer = new Producer();
                l=rs.getLong(1);
                }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return l;
    }

    public void remove() throws SQLException {
        Connection con = null;
        Statement stmt = null;
        try {
            con = DriverManager.getConnection(url, login, password);
            stmt = con.createStatement();
            stmt.executeUpdate("DELETE FROM Manufacturer");
        } finally {
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
    }

}
